package com.ruoyi.lxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.lxy.service.ITSelectUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lxy.domain.bo.TPolicyBo;
import com.ruoyi.lxy.domain.vo.TPolicyVo;
import com.ruoyi.lxy.domain.TPolicy;
import com.ruoyi.lxy.mapper.TPolicyMapper;
import com.ruoyi.lxy.service.ITPolicyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 政策Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class TPolicyServiceImpl implements ITPolicyService {

    private final TPolicyMapper baseMapper;
    private final ITSelectUserService selectUserService;

    /**
     * 查询政策
     */
    @Override
    public TPolicyVo queryById(Long id) {
        System.out.println(baseMapper.selectVoById(id));
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询政策列表
     */
    @Override
    public TableDataInfo<TPolicyVo> queryPageList(TPolicyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TPolicy> lqw = new LambdaQueryWrapper<>();
        Page<TPolicyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 修改政策
     */
    @Override
    public Boolean updateByBo(TPolicyBo bo) {
        //重置轮数，取消所有报名限制
        if (bo.getNeedRe0() == 0) selectUserService.resetSelectUser();
        TPolicy update = BeanUtil.toBean(bo, TPolicy.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TPolicy entity) {
        //TODO 做一些数据校验,如唯一约束
    }

}
